package algorithm_optimal_solution.LinkedList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//单链表按某值划分成左小、中等、右大形式：使用Node数组，设置左右两个下标，进行遍历比较替换，最后将排好序的数组替换。
//进阶（做到有序，空间复杂度O(1)）：设置6个节点用于按序保存节点，小头，小尾，等头，等尾，大头，大尾。最后连接即可。
public class DivideLinkedList {

    static class Node{
        private int value;
        private Node node;

        public Node(int value,Node node){
            this.value = value;
            this.node = node;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getNode() {
            return node;
        }

        public void setNode(Node node){
            this.node = node;
        }
    }

    public void divideLinkedList(Node head,int len,int pivot){
        Node node = head;
        Node[] arr = new Node[len];
        for(int i = 0;i < len;i++){
            arr[i] = node;
            node = node.getNode();
        }
        int less = -1;
        int greater = len;
        int index = 0;
        while(index != greater){
            if(arr[index].getValue() < pivot){
                swap(arr,++less,index++);
            }else if(arr[index].getValue() == pivot){
                index++;
            }else{
                swap(arr,--greater,index);
            }
        }
        for(int i = 1;i < len;i++){
            arr[i - 1].setNode(arr[i]);
        }
        arr[len - 1].setNode(null);
        head = arr[0];
        StringBuilder sb = new StringBuilder();
        while(head != null){
            sb.append(head.getValue()).append(" ");
            head = head.getNode();
        }
        System.out.print(sb);
    }

    private void swap(Node[] arr,int cur,int next){
        Node temp = arr[cur];
        arr[cur] = arr[next];
        arr[next] = temp;
    }

    public void divideLinkedList(Node head,int pivot){
        Node lessHead = null;
        Node lessTail = null;
        Node equalHead = null;
        Node equalTail = null;
        Node greaterHead = null;
        Node greaterTail = null;
        Node next = null;
        while(head != null){
            next = head.getNode();
            head.setNode(null);
            if(head.getValue() < pivot){
                if(lessHead == null){
                    lessHead = head;
                    lessTail = head;
                }else{
                    lessTail.setNode(head);
                    lessTail = head;
                }
            }else if(head.getValue() == pivot){
                if(equalHead == null){
                    equalHead = head;
                    equalTail = head;
                }else{
                    equalTail.setNode(head);
                    equalTail = head;
                }
            }else{
                if(greaterHead == null){
                    greaterHead = head;
                    greaterTail = head;
                }else{
                    greaterTail.setNode(head);
                    greaterTail = head;
                }
            }
            head = next;
        }
        if(lessTail != null){
            lessTail.setNode(equalHead);
            equalTail = equalTail == null ? lessTail : equalTail;
        }
        if(equalTail != null){
            equalTail.setNode(greaterHead);
        }
        lessHead = lessHead != null ? lessHead : equalHead != null ? equalHead : greaterHead;
        StringBuilder sb = new StringBuilder();
        while(lessHead != null){
            sb.append(lessHead.getValue()).append(" ");
            lessHead = lessHead.getNode();
        }
        System.out.print(sb);
    }

    public static void main(String[] args)throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        String[] values = scanner.readLine().split(" ");
        int len = Integer.parseInt(values[0]);
        String[] numbers = scanner.readLine().split(" ");
        Node node = null;
        for(int i = len - 1;i >= 0;i--){
            node = new Node(Integer.parseInt(numbers[i]),node);
        }
//        new DivideLinkedList().divideLinkedList(node,len,Integer.parseInt(values[1]));
        new DivideLinkedList().divideLinkedList(node,Integer.parseInt(values[1]));
    }
}
